// 管理题目数据

import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getProblemAPI, getTagsAPI } from '@/apis/problem'
import { ElMessage } from "element-plus"

// 获取题目详情
export const useProblemStore = defineStore('problem', () => {
    const problem = ref({
        id: null,
        title_en: "",
        title_cn: "",
        level: "",
        description: "",
        header: "",
        tail: "",
        cpu_limit: "",
        mem_limit: "",
        tags: []
    })

    const getProblem = async (url) => {
        try {
            const res = await getProblemAPI(url)
            if (res.code === 0) {
                problem.value = res.description
            } else {
                throw Error
            }
        } catch (error) {
            ElMessage({ type: "error", message: "获取题目失败" })
        }
    }

    return { problem, getProblem }
}, {
    persist: [
        {
            storage: sessionStorage
        }
    ]
})


// 获取选项
export const useSelectStore = defineStore('select', () => {
    // type Level = {
    //     level_en: string,
    //     level_cn: string,
    // }

    // type Tag = {
    //     tag_en: string,
    //     tag_cn:string
    // }
    // 保存标签选项
    const selectedTags = ref([]) // ref<Tag>()

    const selectedLevel = ref() // ref<Level>()

    const allTags = ref([])

    const getAllTags = async () => {
        const res = await getTagsAPI()
        if (res.code === 0) {
            // 请求成功
            allTags.value = res.tags
        }
    }

    const allLevels = [
        {
            level_en: "easy",
            level_cn: "简单",
        },
        {
            level_en: "medium",
            level_cn: "中等",
        },
        {
            level_en: "hard",
            level_cn: "困难",
        },
    ]

    const clearSelectedTags = () => {
        selectedTags.value = []
    }

    const clearSelectedLevel = () => {
        selectedLevel.value = null
    }
    return { selectedTags, selectedLevel, allLevels, allTags, getAllTags, clearSelectedTags, clearSelectedLevel }
}, {
    persist: [
        {
            storage: sessionStorage
        }
    ]
})